package com.yjzx.util.service.minio;

import com.yjzx.util.service.minio.service.FileUploadService;
import com.yjzx.util.service.minio.service.impl.FileUploadServiceImpl;

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author yjzx
 * @date 2024/05/27
 * @description minio配置
 */
@Configuration
//指定类放入容器，自动配置设置
@EnableConfigurationProperties(MinioProperties.class)
public class MinioAutoConfiguration {
    @Autowired
    MinioProperties minioProperties;

    @Bean
    public MinioClient minioClient() throws Exception {
        //1、创建minioClient
        MinioClient client = new MinioClient(minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());

        boolean mall = client.bucketExists(minioProperties.getBucketName());
        //不存在此桶就会创建桶
        if(!mall){
            client.makeBucket(minioProperties.getBucketName());
        }
        return client;
    }


    @Bean
    public FileUploadService fileUploadService(){
        return new FileUploadServiceImpl();
    }
}
